﻿using System;
using System.Threading;
using AOPDemo.Data;
using AOPDemo.Domain;
using AOPDemo.Interfaces;
using AOPDemo.Messages;

namespace AOPDemo.Services
{
    public class AuthenticationService : IAuthenticationService
    {
        private readonly Users users = new Users();

        #region Implementation of IAuthenticationService

        public AuthenticateResponse Authenticate(AuthenticateRequest request)
        {
            Thread.Sleep(1000);
            var response = new AuthenticateResponse
                               {
                                   AuthToken = new AuthToken
                                                   {
                                                       UserId = request.AuthToken.UserId
                                                   }
                               };
            if (users.ContainsKey(request.AuthToken.UserId) &&
                users[request.AuthToken.UserId].UserName == request.UserName &&
                users[request.AuthToken.UserId].Password == request.Password)
            {
                response.AuthToken.SessionId = Guid.NewGuid().ToString();
                response.Success = true;
            }
            return response;
        }

        #endregion
    }
}